package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;

@Cmd(code = Protocol.C_CROSS_UNION_DONATE_SILVER, desc = "商会战捐赠银两")
public class CrossUnionWarDonateSilverCmd extends CrossUnionCommand {

    private int activityId;
    private int stageNo;
    private int roundNo;
    private String unionUid;
    private BigInteger silver;
    private BigInteger totalSilver;
    private UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossUnionWarProto.CrossUnionWarDonateSilverReqMsg reqMsg = CrossUnionWarProto.CrossUnionWarDonateSilverReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        stageNo = reqMsg.getPhase();
        roundNo = reqMsg.getSession();
        unionUid = reqMsg.getUnionUid();
        silver = new BigInteger(reqMsg.getDonateSilver());
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserBaseInfo());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        totalSilver = CrossUnionWarMgr.updateUnionWarDonate(activityId, stageNo, roundNo, unionUid, serverId, packet.getPlayerId(), silver, userBaseInfo);
    }

    @Override
    public void sendData() {
        CrossUnionWarProto.CrossUnionWarDonateSilverRespMsg.Builder respMsg = CrossUnionWarProto.CrossUnionWarDonateSilverRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setPhase(stageNo);
        respMsg.setSession(roundNo);
        respMsg.setUnionUid(unionUid);
        respMsg.setTotalDonateSilver(totalSilver.toString());
    }
}
